// 导入React
import React from "react";

/**
 * 目标：了解什么是组件的状态
 * 创建一个函数组件或者类组件，显示当前时间
 */

class App extends React.Component{

    constructor(props) {
        super(props)
    
        this.state = {
             time: new Date().toLocaleTimeString()
        }
    }
    
    componentDidMount(){
      this.timerId = setInterval(()=> {
        this.setState({
          time: new Date().toLocaleTimeString()
        })
      }, 1000)
      console.log(this.timerId)
    }

    render() {
        return (
            <div>
                {/* 点击按钮获取当前时间 */}
                <h1>当前时间是：{this.state.time}</h1>
            </div>
        )
    }

    componentWillUnmount(){
      clearInterval(this.timerId)
    }

}

export default App

